//
//  UserSettingController.m
//  SmallElephant
//
//  Created by Faith on 2022/12/24.
//

#import "UserSettingController.h"
#import "Profile/UserSettingProfileController.h"

@interface UserSettingController ()

/// 个人资料视图
@property (nonatomic, strong) UIView *profileView;

/// 其他信息视图
@property (nonatomic, strong) UIView *otherView;

@end

@implementation UserSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置标题
    [self setTitleText:@"设置"];
    // 添加个人资料
    [self.scrollViewContent addSubview:self.profileView style:@"display: flex; flex-direction: column; border-radius: 10px; background: #FFFFFF; margin-left: 10px; margin-right: 10px; margin-top: 10px;"];
    // 添加其他信息视图
    [self.scrollViewContent addSubview:self.otherView style:@"display: flex; flex-direction: column; border-radius: 10px; background: #FFFFFF; margin-left: 10px; margin-right: 10px; margin-top: 10px;"];
    // 退出登录
    UIButton *btnLogout = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLogout setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.scrollViewContent addSubview:btnLogout style:@"display: flex; border-radius: 10px; background: #FFFFFF; margin-left: 10px; margin-right: 10px; margin-top: 10px; color: #EF2020; height: 60px; font-size: 15px;"];
    // 加载数据
    [self loadData];
}

/// 构建列表组件
- (UIView *)buildList:(NSArray*)itemList {
    UIView *contentView = [[UIView alloc] init];
    for(int i=0; i<itemList.count; i++) {
        NSDictionary *itemData = itemList[i];
        
        UIView *itemView = [[UIView alloc] init];
        itemView.customData = itemData;
        itemView.userInteractionEnabled = YES;
        [itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnItemTapEventHandler:)]];
        [contentView addSubview:itemView style:@"display: flex; flex-direction: row; justify-content: space-between; padding: 15px; "];
        
        // 左边视图
        UIView *profileLeftView = [[UIView alloc] init];
        [itemView addSubview:profileLeftView style:@"display: flex; flex-direction: column;"];
        
        UILabel *profileTitleView = [[UILabel alloc] init];
        profileTitleView.text = itemData[@"title"];
        [profileLeftView addSubview:profileTitleView style:@"font-size: 15px; color: #333333;"];
        
        UILabel *profileHelpTextView = [[UILabel alloc] init];
        profileHelpTextView.text = itemData[@"helpText"];
        [profileLeftView addSubview:profileHelpTextView style:@"margin-top: 5px; font-size: 13px; color: #888888;"];
        
        // 右边视图
        UIView *profileRightView = [[UIView alloc] init];
        [itemView addSubview:profileRightView style:@"display: flex; flex-direction: row; justify-content: flex-end; align-items: center;"];
        if(itemData[@"value"]) {
            // 项目值视图
            UILabel *profileValueView = [[UILabel alloc] init];
            profileValueView.text = itemData[@"value"];
            [profileRightView addSubview:profileValueView style:@"font-size: 13px; color: #888888; margin-right: 10px;"];
        }
        // 箭头视图
        UIButton *profileArrowView = [UIButton buttonWithType:UIButtonTypeCustom];
        [profileArrowView setImage:[UIImage systemImageNamed:@"chevron.right"] forState:UIControlStateNormal];
        profileArrowView.tintColor = [UIColor colorWithHexString:@"#BCBCBC"];
        [profileRightView addSubview:profileArrowView style:@"width: 24px; height: 24px;"];
        
        // 分割线
        if(i<itemList.count - 1) {
            UIView *sepratorLineView = [[UIView alloc] init];
            [contentView addSubview:sepratorLineView style:@"display: flex; border-width: 1px; border-color: #F0F0F0; height: 1px; margin-left: 15px; margin-right: 15px;"];
        }
    }
    return contentView;
}

/// 懒加载个人资料
- (UIView *)profileView {
    if(!_profileView) {
        NSArray *profileItemData = @[
            @{
                @"title": @"个人信息", @"helpText": @"修改昵称、头像、账户名"
            },
            @{
                @"title": @"手机号码", @"helpText": @"若手机更换请尽快修改", @"value": @"13800138000"
            },
            @{
                @"title": @"登录密码", @"helpText": @"建议您定期更改登录密码以保护账户安全"
            },
            @{
                @"title": @"支付密码", @"helpText": @"用于平台余额支付验证"
            }
        ];
        _profileView = [self buildList:profileItemData];
    }
    return _profileView;
}

/// 懒加载其他信息视图
- (UIView *)otherView {
    if(!_otherView) {
        NSArray *otherItemData = @[
            @{
                @"title": @"设备管理", @"helpText": @"管理您近期登陆过的设备记录"
            },
            @{
                @"title": @"注销账号", @"helpText": @"账号一旦注销，将无法登录，请谨慎操作"
            }
        ];
        _otherView = [self buildList:otherItemData];
    }
    return _otherView;
}

/// 项目点击事件
- (void)btnItemTapEventHandler:(UITapGestureRecognizer*)recognizer {
    NSDictionary *itemData = recognizer.view.customData;
    if([itemData[@"title"] isEqualToString:@"个人信息"]) {
        UserSettingProfileController *controller = [[UserSettingProfileController alloc] init];
        [self pushViewController:controller];
    }
}

/// 加载数据
- (void)loadData {
    
}


@end
